package com.atguigu4.search_sort.exer2;

/**
 * @author lhl
 * @date 2025/3/31 14:09
 */
public class LinearSearchTest {
    public static void main(String[] args) {
        int[] arr1 = new int[]{34,54,3,2,65,7,34,5,76,34,67};

        int target = 5;

        //查找方式：线性查找
        //方式1
//        boolean isFlag = true;
//        for (int i = 0; i < arr1.length; i++) {
//            if (arr1[i] == target) {
//                System.out.println("找到了"+target+"索引为："+i);
//                isFlag = false;
//                break;
//            }
//        }
//
//        if (isFlag){
//            System.out.println("不好意思，没找到此元素");
//        }

        //方式2
        int i = 0;
        for (; i < arr1.length; i++) {
            if (arr1[i] == target) {
                System.out.println("找到了"+target+"索引为："+i);
                break;
            }
        }

        if (i == arr1.length) {
            System.out.println("不好意思，没找到此元素");
        }
    }
}
